<?php

namespace Admin\Controller;

class ShopController extends AdminController {
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',-1);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('shop',$map,'id');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '商品列表';
        $this->display(); 
    }

    public function add(){
           if(IS_POST){
            $Shop = D("shop");
            //dump(I("post."));
            $res = $Shop->create(I("post."));
            //dump($res);
            if($res){
                if($Shop->add($res)){
                    //dump($Shop->getLastSql());die;
                    $this->success('添加成功',U('index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error(D("shop")->getError());
            }
        }else{
            $res=D('cate')->where(array('fid'=>0))->select();
            $this->assign('res',$res);
            $this->display();
        }
    }
    
    public function ajaxGet(){
        $id=I('get.id');
        if($id){
            $res=M('cate')->where(array('fid'=>$id))->select();
            if($res){
                $this->assign('res',$res);
                $html=$this->fetch('ajax');
                $return['status']=1;
                $return['info']="success";
                $return['data']=$html;
            }else{
                $return['status']=0;
                $return['info']="error";
                $return['data']='';
            }
        $this->ajaxReturn($return);
        }
    }    


    public function del(){
        $id = I('get.id');
        $res = D('Shop')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }


    public function update(){
        if(IS_POST){
            $Shop = D("shop");
            $img = $this->upload(); 
            $img=$img["img"];
            //dump($img);

            $a.='/Uploads/'.$img['savepath'].$img['savename'];
            //dump($a);die;
            $post=I('post.');
            $post['img']=$a;
            //dump($post['img']);die;
            //dump($post);die;
            if($Shop->save($post)){             
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');          
            }
        }else{
            $Shop = D("shop");
            $res =$Shop->where(array('id'=>I("get.id")))->find();
            $this->assign('res',$res);
            $this->assign('img',$img);
            $this->display();
        }
    }



    

    public function upload(){
        //dump($_FILES["img"]);die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     '/Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        //$upload->saveName  =     '';// 采用时间戳命名
     
        // 上传文件 
        $info   =   $upload->upload();
        //dump($info);
        if(!$info) {// 上传错误提示错误信息
             $this->error($upload->getError());
        }else{// 上传成功
             //$this->success('上传成功！');      
             return $info;
        }
    }


    


}

